﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.ServiceModel.Description;
using WcfContrib.Errors;
using WcfContrib.Hosting;

namespace WcfContrib.Showcase.Service
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //There is also a ServiceHostFactory for use within IIS

                ServiceHost[] hosts = new ServiceHost[]
                {
                    new ServiceHost<Service>(),

                    ServiceConfigurationDescription
                        .CreateDefault(2022, 2023, KnownEndpointConfiguration.NetTcp, "MyService1")
                        .GenerateServiceHost<ServiceConfigFree1>(),

                    ////////////////////////////////
                    //NEW FROM 2.0:
                    //This simple set increases the max items in object graph of the client endpoint as well as boosts the binding - increases max received message size, buffer sizes and reader quotas.
                    //This property is of the WcfContrib.Hosting.ServiceHost, you can set it directly on the host even if you're not using the ServiceConfigurationDescription
                    ////////////////////////////////
                    ////////////////////////////////
                    //NEW FROM 2.1.0.0:
                    //You can define the apply boosting in the new configuration section and it will be applied to all client channels unless specified otherwise per-instance
                    ////////////////////////////////
                    ServiceConfigurationDescription.Create("MyService2")
                        .WithBasicHttp(2023)
                        .WithBasicHttp("anon", KnownSecurityMode.None)
                        .WithWsHttp("ws")
                        .WithNetTcp(2022, KnownEndpointConfiguration.NetTcp)
                        .WithNetTcp("rs", KnownEndpointConfiguration.NetTcpReliableSession)
                        .WithWebHttp("web", KnownEndpointConfiguration.WebHttp)
                        .WithWebHttp("webScript", KnownEndpointConfiguration.WebHttpScriptEnabled)
                        .MakeDiscoverable()
                        .GenerateServiceHost<ServiceConfigFree2>(h => h.ApplyBoosting = true)
                };

                Console.ForegroundColor = ConsoleColor.Cyan;
                foreach (var host in hosts)
                {
                    host.Open();

                    string eps = host.Description.Endpoints.Select(
                    e => string.Format("Endpoint: Address - {0}\r\n          Binding - {1}", e.Address, e.Binding.GetType().Name)).Aggregate((s1, s2) => s1 + "\r\n\r\n" + s2);
                    
                    Console.WriteLine("\r\n--------- {0} ---------", host.Description.Name);
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine(eps);
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine("------------------------------");
                }
                
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine("Service is up, press 'Enter to exit'");
                Console.ReadLine();

                foreach (var host in hosts)
                {
                    host.Close();
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Error: " + ex.ToString());
                Console.ReadLine();
            }
        }
    }
}
